package com.wctj.api.business.common.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author carol
 */
public class CustomDateDeserializer extends JsonDeserializer<Date> {
    private final DateTimeFormatter formatter;

    public CustomDateDeserializer(DateTimeFormatter formatter) {
        this.formatter = formatter;
    }

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String dateStr = p.getValueAsString();
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateStr, formatter);
        return Date.from(zonedDateTime.toInstant());
    }
}